<!doctype html>
<html>
<title>form提交密码js加密页面2</title>
  <head>
    <title>JavaScript RSA Encryption</title>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script src="jsencrypt.js"></script>
    <script type="text/javascript">
// 使用jsencrypt类库加密js方法，
function encryptRequest(reqUrl, data, publicKey){
    var encrypt = new JSEncrypt();
    encrypt.setPublicKey(publicKey);
    // ajax请求发送的数据对象
    var sendData = new Object();
    // 将data数组赋给ajax对象
    for(var key in data){
        sendData[key] = encrypt.encrypt(data[key]);
		alert(sendData[key]);
    }
 
      $.ajax({
            url: reqUrl,
            type: 'post',
            data: sendData,
            dataType: 'json',
            success: function (data) {                    
                console.info(data);
            },
            error: function (xhr) {
                console.error('出错了');
            }
        });
 
}
 
      // Call this code when the page is done loading.
      $(function() {
 
        $('#testme').click(function() {
             
            var data = [];
            data['username']= $('#username').val();
            data['passwd']= $('#passwd').val();

            var pkey = $('#pubkey').val();
			
			 
			 
            encryptRequest('./test.php', data, pkey);
        });
      });
    </script>
  </head>
  <body>
    <label for="pubkey">Public Key</label><br/>
    <textarea id="pubkey" rows="15" cols="65">-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBWNoG5LJ3u44Gs8PWs1MaNUQQ
+mOmh+9zWdzSt3ORbmfCDvU+ssW/6QTTgXvWWx7+Wzq/a4fCCQp72zSqXeVhWkTV
ct9Hyp/iMo5K6qOEK76z9z+tP/u99X6qazeXGVMWKkPiyZT4mKAGd/U8Mph9Z1Z5
kOluA7g7heq8PPlE9wIDAQAB
-----END PUBLIC KEY-----</textarea><br/>
    <label for="input">Text to encrypt:</label><br/>
    name:<input id="username" name="username" type="text"></input><br/>
    password:<input id="passwd" name="passwd" type="password"></input><br/>
    <input id="testme" type="button" value="submit" /><br/>
  </body>
</html>